package test;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class HomePage extends Utils {
    private static String URL = "http://113.44.92.31:65533/index.html";
    public HomePage() {
        super(URL);
    }

    /**
     * 测试主页导航栏功能
     * @throws InterruptedException
     */
    public void checkNavigation() throws InterruptedException {
        //1.测试搜索栏
        assert driver.findElement(By.cssSelector("#index_search_input")).getAttribute("placeholder").equals("输入关键字...");
        element = driver.findElement(By.cssSelector("#index_search_input"));
        new Actions(driver).click(element).perform();
        driver.findElement(By.cssSelector("#index_search_input")).sendKeys("java");
        //键盘事件-回车
        driver.findElement(By.cssSelector("#index_search_input")).sendKeys(Keys.ENTER);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#search-title")));
        //显示查找结果，并进行结果跳转文章详情
        driver.findElement(By.cssSelector("#search-title"));
        Thread.sleep(1000);
        driver.findElement(By.cssSelector("#search-results > div > div > div > div > div > span:nth-child(1)"));
        driver.findElement(By.cssSelector("#search-results > div > div > div > div > div > span.ms-2"));
        driver.findElement(By.cssSelector("#search-results > div > div > div > div > a")).click();
        //通过判断点击返回主页的按钮来判断是否跳转文章详情页，并判断是否能够跳转回主页
        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div.row.align-items-center > div.col-auto.btn-list > a:nth-child(2) > svg")));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div.row.align-items-center > div.col-auto.btn-list > a:nth-child(2) > svg")).click();
        //并判断是否能够跳转回主页
        Thread.sleep(1000);
        driver.findElement(By.cssSelector("#article_list_board_title"));
        //2.测试模式切换
        assert driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > a.nav-link.px-0.hide-theme-dark")).getAttribute("data-bs-original-title").equals("切换至夜间模式");
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > a.nav-link.px-0.hide-theme-dark")).click();
        assert driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > a.nav-link.px-0.hide-theme-light")).getAttribute("data-bs-original-title").equals("切换至白天模式");
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > a.nav-link.px-0.hide-theme-light")).click();
        //3.测试站内信息
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div > a")).click();
        Thread.sleep(1000);
        assert driver.findElement(By.cssSelector("#offcanvasEndLabel")).getText().equals("所有站内信");
        driver.findElement(By.cssSelector("#index_message_offcanvasEnd > div.offcanvas-header > button")).click();
        //4.测试头像
        //driver.findElement(By.cssSelector("#index_nav_avatar")).sendKeys("F:\\图片\\autoTest.jpg");
        //你这个比较特殊 是放到span的css中指定的图片url路径 网上搜了一下解决方法 试一下
        // 等待元素并修改图片
        //项目是跑在服务器上的 图片是本地的 提交上去 刷新头像又没了 项目本身的前端代码 改不了的 你刷新一下 头像又回去了
        /*WebElement avatar = new WebDriverWait(driver, Duration.ofSeconds(10))
                .until(ExpectedConditions.presenceOfElementLocated(By.id("index_nav_avatar")));

        String newImagePath = "../images/autoTest.jpg";
        ((JavascriptExecutor)driver).executeScript(
                "arguments[0].style.backgroundImage = 'url(\"" + newImagePath + "\")'",
                avatar
        );
        // 验证修改
        String updatedStyle = avatar.getAttribute("style");
        System.out.println("------------------------------------------------");
        System.out.println("Updated style: " + updatedStyle);
        Thread.sleep(3000);
         */
        //fail
        //点击姓名->我的帖子 个人中心 退出
        driver.manage().window().maximize();
        //我的帖子
        Thread.sleep(1000);
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown > a > div")).click();
        Thread.sleep(1000);
        driver.findElement(By.cssSelector("#index_user_profile")).click();
        Thread.sleep(1000);
        //点击我的第一篇帖子
        driver.findElement(By.cssSelector("#profile_article_body > li:nth-child(1) > div.card.timeline-event-card > div > div > div > div > div.text-truncate > a")).click();
        driver.findElement((By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.btn-list > a:nth-child(2)"))).click();
        Thread.sleep(1000);
        //个人中心
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown > a > div")).click();
        Thread.sleep(1000);
        driver.findElement(By.cssSelector("#index_user_settings")).click();
        Thread.sleep(1000);
        //修改头像
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div.col.d-flex.flex-column > div > div.row.align-items-center > div:nth-child(2) > a")).sendKeys("F:\\图片\\autoTest.jpg");
        /*
        // 输入文件路径
        WebElement fileInput = driver.findElement(By.cssSelector("文件输入框的选择器"));
        fileInput.sendKeys("F:\\图片\\autoTest.jpg");

        // 点击提交按钮
        WebElement submitButton = driver.findElement(By.cssSelector("提交按钮的选择器"));
        submitButton.click();

        // 验证头像是否更新
        WebElement avatar = driver.findElement(By.cssSelector("头像显示的选择器"));
        String avatarSrc = avatar.getAttribute("src");
        System.out.println("Avatar src: " + avatarSrc);
         */

        Thread.sleep(1000);
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.btn-list > a:nth-child(2)")).click();
        Thread.sleep(1000);
        driver.findElement(By.cssSelector("#index_nav_nickname")).click();
        driver.findElement(By.cssSelector("#index_user_logout")).click();
        Thread.sleep(1000);
        driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("Cyan");
        driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("1111");
        driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
        Thread.sleep(1000);
        String afterURL = driver.getCurrentUrl();
        assert afterURL.equals(URL);
    }

    public void checkMainPage() throws InterruptedException {
        Thread.sleep(1000);
        //点击查看热门帖子
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div.col-lg-3 > div > div.text-center.py-3.featured-hint > span")).click();
        //收起
        Thread.sleep(1000);
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div.col-lg-3 > div > div.card-header.d-flex.justify-content-between.align-items-center > button")).click();
        //展开
        Thread.sleep(1000);
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div.col-lg-3 > div > div.card-header.d-flex.justify-content-between.align-items-center > button")).click();
        //点击热门帖子
        Thread.sleep(1000);
        driver.findElement(By.cssSelector("#featured-articles > a:nth-child(2)")).click();
        //返回主页
        Thread.sleep(1000);
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div.row.align-items-center > div.col-auto.btn-list > a:nth-child(1)")).click();
        //点击首页发布的第一个帖子
        Thread.sleep(1000);
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a")).click();
        Thread.sleep(500);
        //返回主页
        Thread.sleep(1000);
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div.row.align-items-center > div.col-auto.btn-list > a:nth-child(1)")).click();
    }
    /**
     * 测试翻页是否正常
     * @throws InterruptedException
     */
    public void checkNextPage() throws InterruptedException {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        Thread.sleep(500);
        element = driver.findElement(By.cssSelector("#artical-items-body > div.d-flex.justify-content-between.align-items-center.mt-4 > ul > li:nth-child(4) > a"));
        Thread.sleep(100);
        js.executeScript("arguments[0].scrollIntoView(true);",element);
        Thread.sleep(1500);
        element.click();
        Thread.sleep(500);
       driver.findElement(By.cssSelector("#artical-items-body > div.d-flex.justify-content-between.align-items-center.mt-4 > ul > li:nth-child(1) > a")).click();
    }
}
